package cn.edu.nit.sys.controller;

import cn.edu.nit.sys.entity.Subject;
import cn.edu.nit.sys.service.SubjectService;
import cn.edu.nit.sys.utils.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author xiaobai
 * @Date 2023/3/19 15:00
 * @Version 1.0.0
 */
@RestController
@RequestMapping("/subject")
public class SubjectController {

    @Autowired
    private SubjectService subjectService;

    @PostMapping
    public R addSubject(@RequestBody Subject subject) {
        subject.setDuration(subject.getTimeStr().split(",").length * 2);
        return subjectService.save(subject) ? R.ok("添加成功") : R.error("添加失败");
    }

    @GetMapping
    public R getSubjectList(Page<Subject> page, String siftClazz, String siftName, String siftId) {
        return subjectService.getSubjectList(page, siftClazz, siftName, siftId);
    }

    @PutMapping
    public R updateSubject(@RequestBody Subject subject) {
        return subjectService.updateById(subject) ? R.ok("修改成功") : R.error("修改失败");
    }

    @DeleteMapping("/{id}")
    public R delSubject(@PathVariable("id") String id) {
        return subjectService.removeById(id) ? R.ok("删除成功") : R.error("删除失败");
    }

    @PostMapping("/sign")
    public R addSign(@RequestBody Map<String, String> params) {
        return subjectService.addSign(params);
    }

    @GetMapping("/sign")
    public R getList(Page<Map<String, String>> page ,String id,  String type) {
        return subjectService.getSignList(page, id, type);
    }

    @GetMapping("/sign/record")
    public R getSignRecord(String id) {
        return subjectService.getSignRecord(id);
    }

    @PostMapping("/sign/log")
    public R addSignLog(@RequestBody Map<String, String> params) {
        return subjectService.addSignLog(params);
    }

    @GetMapping("/sign/exist")
    public R isSigned(String id, String uid) {
        return subjectService.isSigned(id, uid);
    }

    @GetMapping("/sign/list")
    public R getSignRecord(Page<Map<String, String>> page, String siftName, String id) {
        return subjectService.getSignRecord(page, siftName, id);
    }
}
